<?php
namespace English\Controller;

use Think\Page;
use app\English\Controller\Base;
class DuihuanController extends BaseController
{
    /**
     * 今日汇率
     * User: ming
     * Date: 2020/4/8 17:58
     */
    public function exchange(){
        $config = M('reward_config')->where('id=1')->find();
        $this->assign('config',$config);
        $this->display();
    }

    /**
     * 兑汇
     * User: ming
     * Date: 2020/4/8 17:59
     */
    public function transfers(){
        $config = M('reward_config')->where('id=1')->find();
        $this->assign('config',$config);
        $this->display();
    }

    /**
     * @Notes: 闪兑
     * @author: Qian
     * @Time: 2020/4/11 14:25
     */
    public function index()
    {
        //系统参数
        $config=M('reward_config')->find();
        $this->assign('config',$config);
        //会员信息
        $UserInfo = M('user')->where(['id'=>$this->user_info['id']])->find();
        $this->assign('UserInfo',$UserInfo);
        if (IS_POST){

            //判断字段信息收购完整
            $param = I('post.');
            if ($param['from']<=0 || !is_numeric($param['from'])){
                $this->ajaxreturn(array('code' => 0, 'message' => 'Numbers are illegal'));
            }
            if (!in_array($param['from_coin'],[1,2,8]) || !in_array($param['to_coin'],[1,2,8])){
                $this->ajaxreturn(array('code' => 0, 'message' => 'The currency is illegal'));
            }

            if($UserInfo['z2']<$config['a51']){
                $this->ajaxreturn(array('code' => 0, 'message' => 'Miner fee is insufficient, use USDT quick flash DACM'));
            }

            $all_asset=$config['a62']*$UserInfo['z14'];//3倍资产包
            $leiji = $UserInfo['z18'];
            $left_asset = $all_asset - $leiji; //剩余的资产包
            if ($param['from_coin']==1){
                if($left_asset<=0){
//                $this->ajaxreturn(array('code' => 0, 'message' => '不能闪兑，需要复投'));
                    $this->ajaxreturn(array('code' => -1, 'message' => 'You have exceeded the asset range value, please transfer the asset package with one key！'));
                }

                //sweet
                //2020 11 24 14:33  30个sweet起兑
                if ($param['from']<$config['exchange_least_sweet']){
                    $this->ajaxreturn(array('code' => 0, 'message' => $config['exchange_least_sweet'].'个SWEET起兑'));
                }
                if ($param['to_coin']==2){
                    //sweet兑换dacm
                    $this->ajaxreturn(array('code' => 0, 'message' => 'Channel temporarily closed'));
                    $price = $config['p1'];
                    $fee_percent = $config['fee1']/100;
                    $fee = round($param['from']*$fee_percent,4);
                    $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
                }elseif ($param['to_coin']==8){
                    $locked_list_val=M('locked')->where(['userid' => $UserInfo['id'],'status' => 0])->find();
                    if(!$locked_list_val){
                        $this->ajaxreturn(array('code' => 0, 'message' => "If you don't have an asset package, you can't convert"));
                    }
                    //sweet兑换usdt
                    $price =$config['p3'];
                    $fee_percent = $config['fee3']/100;
                    $to_exchange_num = $param['from']*(1-$fee_percent)*$price;

                    if($to_exchange_num>=$left_asset){
//                         $differ = $to_exchange_num - $all_asset; //差额
                        $to_exchange_num = $left_asset; //实际兑换金额
                        $param['from'] = $to_exchange_num/$price; //实际from金额
                    }
                    $fee = round($param['from']*$fee_percent,4);

                    $user['z18'] = $UserInfo['z18'] + $to_exchange_num;//累积闪兑数量20210528
                }

            }elseif ($param['from_coin']==2){
                //dacm
                //2020 11 24 14:33  30个dacm起兑
//                 if ($param['from']<$config['exchange_least_dacm']){
//                     $this->ajaxreturn(array('code' => 0, 'message' => $config['exchange_least_dacm'].'个DACM起兑'));
//                 }
                if ($param['to_coin']==1){
                    //dacm兑换sweet
                    $this->ajaxreturn(array('code' => 0, 'message' => '通道暂时关闭'));
                    $price = $config['p2'];
                    $fee_percent = $config['fee2']/100;
                    $fee = round($param['from']*$fee_percent,4);
                    $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
                }elseif ($param['to_coin']==8){
                    //dacm兑换usdt
                    $price = $config['p5'];
                    $fee_percent = $config['fee5']/100;
                    $fee = round($param['from']*$fee_percent,4);
                    $to_exchange_num = $param['from']*(1-$fee_percent)*$price;

//                     if($to_exchange_num>=$left_asset){
////                         $differ = $to_exchange_num - $all_asset; //差额
//                         $to_exchange_num = $left_asset; //实际兑换金额
//                         $param['from'] = $to_exchange_num/$price; //实际from金额
//                     }
                }
            }else{
                //usdt
//                if($param['from']>=$left_asset){
////                         $differ = $to_exchange_num - $all_asset; //差额
////                    $to_exchange_num = $left_asset; //实际兑换金额
//                    $param['from'] = $left_asset; //实际from金额
//                }
                //2020 11 24 14:33  30个dacm起兑
                if ($param['from']<$config['exchange_least_usdt']){
                    $this->ajaxreturn(array('code' => 0, 'message' => $config['exchange_least_usdt'].'个USDT起兑'));
                }
                if ($param['to_coin']==1){
                    //usdt兑换sweet
                    $price = $config['p4'];
                    $fee_percent = $config['fee4']/100;
                    $fee = round($param['from']*$fee_percent,4);
                    $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
                }elseif ($param['to_coin']==2){
                    //usdt兑换dacm
                    $price = $config['p6'];
                    $fee_percent = $config['fee6']/100;
                    $fee = round($param['from']*$fee_percent,4);
                    $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
                }
            }


            if(($param['from'])>$UserInfo['z'.$param['from_coin']]){
                $this->ajaxreturn(array('code' => 0, 'message' => 'Your currency balance is insufficient'));
            }
            //添加矿工费
            if ($param['from_coin']==2){
                if(($param['from']+$config['a51'])>$UserInfo['z'.$param['from_coin']]){
                    $this->ajaxreturn(array('code' => 0, 'message' => 'Miner fee is insufficient, use USDT quick flash DACM'));
                }

            }
            $exchange['user_id']=$UserInfo['id'];
            $exchange['from_coin']=$param['from_coin'];
            $exchange['from_coin_num']=$param['from'];
            $exchange['to_coin']=$param['to_coin'];
//            $exchange['to_exchange_num']=round($param['from']/$config['ptr17'],2);
            $exchange['to_exchange_num']=$to_exchange_num;
            $exchange['actual_from_num']=$param['from'];
            $exchange['price']=$price;
            $exchange['fee_percent']=$fee_percent;
            $exchange['fee']=$fee;
            $exchange['status']=2;
            $exchange['create_time']=time();
//            dump($exchange);die;
            $res1=M('exchange')->add($exchange);


//            dump($user['z18'] .'||'.$config['a62']*$UserInfo['z14']);die;
            if($user['z18'] >=$config['a62']*$UserInfo['z14'] && $param['from_coin']==1){//累积起来3万，闪兑最大值超过3倍
                $lock_list = M('locked')->where(['userid' => $UserInfo['id'],'status' => 0,'amount'=>['gt',0]])->select();
                foreach($lock_list as $key=>$vo){
                    $time=3600*24*30;
                    if(($vo['addtime']+$time*5)<time()){
                        $charge =$config['a19'];
                    }elseif (($vo['addtime']+$time*4)<time()){
                        $charge =$config['a18'];
                    }elseif (($vo['addtime']+$time*3)<time()){
                        $charge =$config['a17'];
                    }elseif (($vo['addtime']+$time*2)<time()){
                        $charge =$config['a16'];
                    }elseif (($vo['addtime']+$time)<time()){
                        $charge =$config['a15'];
                    }else{
                        $charge =$config['a14'];
                    }
//                    $balance = $vo['amount']*(1 - $charge);
                    $balance_money = $vo['amount']*(1 - $charge);
                    $price_from='z8';

                    //资产包等级
                    if ($balance_money >=50000){
                        $param['type'] = 8;
                    }elseif($balance_money>=40000&&$balance_money<50000){
                        $param['type'] = 7;
                    }elseif($balance_money>=30000&&$balance_money<40000){
                        $param['type'] = 6;
                    }elseif($balance_money>=20000&&$balance_money<30000){
                        $param['type'] = 5;
                    }elseif($balance_money>=10000&&$balance_money<20000){
                        $param['type'] = 4;
                    }elseif($balance_money>=5000&&$balance_money<10000){
                        $param['type'] = 3;
                    }elseif($balance_money>=1000&&$balance_money<5000){
                        $param['type'] = 2;
                    }elseif($balance_money>=500&&$balance_money<1000){
                        $param['type'] = 1;
                    }else{
                        $param['type'] =0;
                    }
                    M('locked')->where(['id' => $vo['id']])->setDec('amount',$vo['amount']);//扣除usdt
//                    M('locked')->where(['id' => $vo['lid']])->setDec('amount',$vo['amount']);//扣除usdt

                    $coin_price=$config['b2']*$vo['amount'];
                    M('locked')->where(['id' => $vo['id']])->setDec('coin_price',$coin_price);//计算sweet值
                    M('locked')->where(['id' => $vo['id']])->save(['type'=>$param['type']]);//更新等级

                    $bj=$balance_money*$config['a39'];//本金归还---扣除部分到股本20201128
                    $gb=$balance_money*(1-$config['a39']);//股本预存数量20201128

                    $arr[$price_from]=$UserInfo[$price_from]+$bj;//本金归还---扣除部分到股本20201128
                    $arr['z17']=$UserInfo['z17']+$gb;//股本预存数量20201128
                    $arr['z16']=$UserInfo['z16']+$gb;//股本总数量20201128
                    $arr['z14']=$UserInfo['z14']-$vo['amount'];
                    if($config['a53']>0){
                        $arr['z2']=$UserInfo['z2']-$config['a53'];
                        $this->add_fmoney($UserInfo['id'],$UserInfo['id'],$config['a53'],53,2,'自动扣除固定DACM');
                    }

                    M('user')->where(['id' => $UserInfo['id']])->save($arr);
                    $this->add_fmoney_log($UserInfo['id'],$UserInfo['id'], $balance_money, 8, '自动本金归还',  "12", $charge,$vo['addtime']);
                    $this->add_fmoney_log($UserInfo['id'], $UserInfo['id'], $gb, 17, '自动理财股本划转',  "16", $charge,$vo['addtime']);

                }
                $amount_usdt= M('locked')->where(['userid'=>$UserInfo['id'],'status'=>0])->sum('amount');
                M('locked')->where(['userid'=>$UserInfo['id'],'status'=>0])->save(['status' => 1]);//已结束
//                $user['z16']=$UserInfo['z16']+$amount_usdt;//资本划转到股本
                $db = M();
                $user_id=$UserInfo['id'];
                $db->execute("call kt002($user_id,$amount_usdt)");
                $db->execute("call kt003_vip($user_id,$amount_usdt)");
                //超过额度
                $db->execute("call up_level($user_id)");

                $user['z19_asset']=$UserInfo['z14'];//资本划转到股本
                $user['z14']=0;//理财资本清空
                $user['z18']=0;//闪兑累积清空
//                M('locked')->where(['userid'=>$UserInfo['id'],'status'=>0])->save(['amount'=>0]);
                $user['sy_status']=1;//不收益状态
            }

            $user['z'.$param['from_coin']]=$UserInfo['z'.$param['from_coin']]-$exchange['actual_from_num'];
            $user['z'.$param['to_coin']]=$UserInfo['z'.$param['to_coin']]+$exchange['to_exchange_num'];
//            $user['z18'] = $UserInfo['z18'] + $to_exchange_num;//累积闪兑数量20210528
//            $user['z18']=$UserInfo['z18']+$exchange['to_exchange_num'];//累积闪兑数量20210528
//            $all_asset=$config['62']*$UserInfo['z14'];//3倍资产包

            $res2=M('user')->where(array('id'=>$UserInfo['id']))->save($user);
            //扣除固定DACM
            $user2= M('user')->where(array('id'=>$UserInfo['id']))->find();
            if($config['a51']>0){
                $user2['z2']=$user2['z2']-$config['a51'];
                if($user2['z2']>0){
                    M('user')->where(array('id'=>$UserInfo['id']))->save($user2);
                    $this->add_fmoney($UserInfo['id'],$UserInfo['id'],$config['a51'],51,2,'扣除固定DACM');
                }

            }
            if($res1&&$res2){
                if($param['from_coin']==8){
                    M('reward_config')->where(array('id'=>1))->setDec('ptr26',$exchange['to_exchange_num']);
                }
//                M()->execute('call auto_shop');
//                $this->add_log($UserInfo['id'],$wfx_num,$param['coin1'],'申请闪兑','-');
                $this->add_fmoney($UserInfo['id'],$UserInfo['id'],$param['from'],$param['from_coin'],$param['from_coin'].'99','申请闪兑');
                $this->ajaxreturn(array('code' => 1, 'message' => 'For successful'));
            }else{
                $this->ajaxreturn(array('code' => 0, 'message' => 'For failure'));
            }
        }

        //去交易表中查询数据
        $exchange_list = M('exchange')->where(['user_id'=>$UserInfo['id']])->order('create_time desc')->select();
        $this->assign('exchange_list',$exchange_list);

//        $url = "https://api.mytokenapi.com/ticker/search?category=currency&keyword=Webflix&timestamp=1558063765845&code=15e926eac6ff75e8664ad3ca93a94a7a&platform=web_pc&v=1.0.0&language=zh_CN&legal_currency=CNY";
//        $param= $this->price_api($url);
//        $param['close']=$param['data'][0]['price'];
//        M('reward_config')->where(array('id'=>1))->save(['hapy'=>$param['close']]);

        $this->display();
    }

//    public function index()
//    {
//        //系统参数
//        $config=M('reward_config')->find();
//        $this->assign('config',$config);
//        //会员信息
//        $UserInfo = M('user')->where(['id'=>$this->user_info['id']])->find();
//        $this->assign('UserInfo',$UserInfo);
//        if (IS_POST){
//
//            //判断字段信息收购完整
//            $param = I('post.');
//            if ($param['from']<=0 || !is_numeric($param['from'])){
//                $this->ajaxreturn(array('code' => 0, 'message' => '数字不合法'));
//            }
//            if (!in_array($param['from_coin'],[1,2,8]) || !in_array($param['to_coin'],[1,2,8])){
//                $this->ajaxreturn(array('code' => 0, 'message' => '币种不合法'));
//            }
//
//            if($UserInfo['z2']<$config['a51']){
//                $this->ajaxreturn(array('code' => 0, 'message' => '矿工费不足，使用USDT快速闪兑DACM'));
//            }
//             if ($param['from_coin']==1){
//                 //sweet
//                 //2020 11 24 14:33  30个sweet起兑
//                 if ($param['from']<$config['exchange_least_sweet']){
//                     $this->ajaxreturn(array('code' => 0, 'message' => $config['exchange_least_sweet'].'个SWEET起兑'));
//                 }
//                 if ($param['to_coin']==2){
//                     $this->ajaxreturn(array('code' => 0, 'message' => '通道暂时关闭~！'));
//                     //sweet兑换dacm
//                     $price = $config['p1'];
//                     $fee_percent = $config['fee1']/100;
//                     $fee = round($param['from']*$fee_percent,4);
//                     $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
//                 }elseif ($param['to_coin']==8){
//                     //sweet兑换usdt
//                     $price =$config['p3'];
//                     $fee_percent = $config['fee3']/100;
//                     $fee = round($param['from']*$fee_percent,4);
//                     $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
//                 }
//
//             }elseif ($param['from_coin']==2){
//                 //dacm
//                 //2020 11 24 14:33  30个dacm起兑
//                 if ($param['from']<$config['exchange_least_dacm']){
//                     $this->ajaxreturn(array('code' => 0, 'message' => $config['exchange_least_dacm'].'个DACM起兑'));
//                 }
//                 if ($param['to_coin']==1){
//                     //dacm兑换sweet
//                     $price = $config['p2'];
//                     $fee_percent = $config['fee2']/100;
//                     $fee = round($param['from']*$fee_percent,4);
//                     $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
//                 }elseif ($param['to_coin']==8){
//                     //dacm兑换usdt
//                     $price = $config['p5'];
//                     $fee_percent = $config['fee5']/100;
//                     $fee = round($param['from']*$fee_percent,4);
//                     $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
//                 }
//             }else{
//                 //usdt
//                 //2020 11 24 14:33  30个dacm起兑
//                 if ($param['from']<$config['exchange_least_usdt']){
//                     $this->ajaxreturn(array('code' => 0, 'message' => $config['exchange_least_usdt'].'个USDT起兑'));
//                 }
//                 if ($param['to_coin']==1){
//                     //usdt兑换sweet
//                     $price = $config['p4'];
//                     $fee_percent = $config['fee4']/100;
//                     $fee = round($param['from']*$fee_percent,4);
//                     $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
//                 }elseif ($param['to_coin']==2){
//                     //usdt兑换dacm
//                     $price = $config['p6'];
//                     $fee_percent = $config['fee6']/100;
//                     $fee = round($param['from']*$fee_percent,4);
//                     $to_exchange_num = $param['from']*(1-$fee_percent)*$price;
//                 }
//             }
//
//
//            if(($param['from'])>$UserInfo['z'.$param['from_coin']]){
//                $this->ajaxreturn(array('code' => 0, 'message' => '您的兑换币余额不足'));
//            }
//            //添加矿工费
//            if ($param['from_coin']==2){
//                if(($param['from']+$config['a51'])>$UserInfo['z'.$param['from_coin']]){
//                    $this->ajaxreturn(array('code' => 0, 'message' => '矿工费不足，使用USDT快速闪兑DACM'));
//                }
//
//            }
//
//            $exchange['user_id']=$UserInfo['id'];
//            $exchange['from_coin']=$param['from_coin'];
//            $exchange['from_coin_num']=$param['from'];
//            $exchange['to_coin']=$param['to_coin'];
////            $exchange['to_exchange_num']=round($param['from']/$config['ptr17'],2);
//            $exchange['to_exchange_num']=$to_exchange_num;
//            $exchange['actual_from_num']=$param['from'];
//            $exchange['price']=$price;
//            $exchange['fee_percent']=$fee_percent;
//            $exchange['fee']=$fee;
//            $exchange['status']=2;
//            $exchange['create_time']=time();
//            // dump($exchange);die;
//            $res1=M('exchange')->add($exchange);
//            $user['z'.$param['from_coin']]=$UserInfo['z'.$param['from_coin']]-$exchange['actual_from_num'];
//            $user['z'.$param['to_coin']]=$UserInfo['z'.$param['to_coin']]+$exchange['to_exchange_num'];
//
//
//            $res2=M('user')->where(array('id'=>$UserInfo['id']))->save($user);
//            //扣除固定DACM
//           $user2= M('user')->where(array('id'=>$UserInfo['id']))->find();
//            if($config['a51']>0){
//                $user2['z2']=$user2['z2']-$config['a51'];
//                if($user2['z2']>0){
//                    M('user')->where(array('id'=>$UserInfo['id']))->save($user2);
//                    $this->add_fmoney($UserInfo['id'],$UserInfo['id'],$config['a51'],51,2,'扣除固定DACM');
//                }
//
//            }
//            if($res1&&$res2){
//                if($param['from_coin']==8){
//                    M('reward_config')->where(array('id'=>1))->setDec('ptr26',$exchange['to_exchange_num']);
//                }
//
////                M()->execute('call auto_shop');
////                $this->add_log($UserInfo['id'],$wfx_num,$param['coin1'],'申请闪兑','-');
//                $this->add_fmoney($UserInfo['id'],$UserInfo['id'],$param['from'],$param['from_coin'],$param['from_coin'].'99','申请闪兑');
//                $this->ajaxreturn(array('code' => 1, 'message' => '兑换成功'));
//            }else{
//                $this->ajaxreturn(array('code' => 0, 'message' => '兑换失败'));
//            }
//        }
//
//        //去交易表中查询数据
//        $exchange_list = M('exchange')->where(['user_id'=>$UserInfo['id']])->order('create_time desc')->select();
//        $this->assign('exchange_list',$exchange_list);
//
////        $url = "https://api.mytokenapi.com/ticker/search?category=currency&keyword=Webflix&timestamp=1558063765845&code=15e926eac6ff75e8664ad3ca93a94a7a&platform=web_pc&v=1.0.0&language=zh_CN&legal_currency=CNY";
////        $param= $this->price_api($url);
////        $param['close']=$param['data'][0]['price'];
////        M('reward_config')->where(array('id'=>1))->save(['hapy'=>$param['close']]);
//
//        $this->display();
//    }

    public function history(){
        //去交易表中查询数据
        $exchange_list = M('exchange')->where(['user_id'=>$this->user_info['id']])->order('create_time desc')->select();
        $this->assign('exchange_list',$exchange_list);
        $this->assign('coinlist',C('COIN_TYPE'));
        $this->display();
    }

    public function coin(){


        if (IS_POST){
            $type= I('post.type');
            $coin= C('COIN_TYPE');
            //获取实时价格
            $type=$coin[$type];
            if($type=="USDT"){
                $url = "https://api.jinse.com/v3/coin/detail?slugs=tether&_source=m";
                $param= $this->price_api($url);
                $param['close']=$param['rate'];
            }elseif ($type=="WFX"){
                $url = "https://api.mytokenapi.com/ticker/search?category=currency&keyword=Webflix&timestamp=1558063765845&code=15e926eac6ff75e8664ad3ca93a94a7a&platform=web_pc&v=1.0.0&language=zh_CN&legal_currency=CNY";
                $param= $this->price_api($url);
                $param['close']=$param['data'][0]['price'];

            }
            else{
                $url = "http://api.coindog.com/api/v1/tick/Binance:".$type."USDT?unit=cny";
                $param= $this->price_api($url);
            }

//            $param= $this->price_api($url,$coin[$type]);


            $this->ajaxreturn(array('code' => 1, 'message' =>$param['close']));
        }
    }
}
